package com.hk.hibernate;

import org.apache.commons.dbcp2.BasicDataSource;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
//import org.hibernate.cfg.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Configuration
@PropertySource("classpath:/com/hk/hibernate/database.property")
public class HConfig {

    @Autowired
    private Environment environment;//注入配置环境


    @Bean(destroyMethod = "close")
    public DataSource dataSource(){

        System.out.println("dataSource");
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUsername(environment.getProperty("jdbc.username"));
        dataSource.setPassword(environment.getProperty("jdbc.password"));
        dataSource.setUrl(environment.getProperty("jdbc.url"));
        dataSource.setDriverClassName(environment.getProperty("jdbc.driverClassName"));
        dataSource.setInitialSize(4);
        dataSource.setInitialSize(1);
        dataSource.setMaxIdle(20);

        return dataSource;
    }


    @Bean
    public LocalSessionFactoryBean sessionFactory(DataSource dataSource){
        System.out.println("sessionFactory");
        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);



        Properties properties = new Properties();
        //properties.put(org.hibernate.cfg.Environment.USER,"lxys");
        //properties.put(org.hibernate.cfg.Environment.PASS,"lxys");
        //properties.put(org.hibernate.cfg.Environment.URL,"jdbc:postgresql://47.93.248.193:5432/lxys");
        //properties.put(org.hibernate.cfg.Environment.DRIVER,"org.postgresql.Driver");
        properties.put(org.hibernate.cfg.Environment.DIALECT,"org.hibernate.dialect.PostgreSQLDialect");

        properties.put(org.hibernate.cfg.Environment.SHOW_SQL,"true");
        sessionFactoryBean.setHibernateProperties(properties);

        return  sessionFactoryBean;
    }


   /*// @Bean
    public SessionFactory sessionFactoryBean(){
        Map<String,String> settings = new HashMap<>();
        settings.put(Environment.USER,"lxys");
        settings.put(Environment.PASS,"lxys");
        settings.put(Environment.URL,"jdbc:postgresql://47.93.248.193:5432/lxys");
        settings.put(Environment.DRIVER,"org.postgresql.Driver");
        settings.put(Environment.DIALECT,"org.hibernate.dialect.PostgreSQLDialect");

        settings.put(Environment.SHOW_SQL,"true");

        StandardServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(settings).build();
        MetadataSources metadataSources = new MetadataSources(registry);
        return  metadataSources.buildMetadata().buildSessionFactory();
    }*/










}
